package SV_EXEC_LOCAL;

public class Vulnerable_01 {
    public static void generateHeapDump() {
        Long processId = getProcessId();
        String jmapPath = getJmapPath();
        String[] dumpCommands = new String[] {jmapCommand,
                "-dump:format=b,file=" + dumpPath,
                processId.toString()};
        try {
            Runtime.getRuntime().exec(dumpCommands);
        } catch (IOException e) {
            logger.error("The heap dump could not be generated due to the following error: ", e);
        }
    }


    private static String getJmapPath() {
        String toolsPath = System.getenv("TOOLS_PATH");
        if (toolsPath == null)
            return null;

        File binDirectory = new File(toolsPath, "bin");
        File[] files = binDirectory.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.startsWith("jmap");
            }
        });

        return ArrayUtils.isEmpty(files) ? null : files[0].getPath();
    }
}
